package br.usjt.component.login.dao;

import java.util.Collections;
import java.util.List;

import javax.persistence.Query;

import br.usjt.component.login.facade.dao.RecursoDao;
import br.usjt.component.login.model.Perfil;
import br.usjt.component.login.model.Recurso;

public class RecursoDaoImp extends GenericDaoImp<Recurso> implements RecursoDao {

    private static final long serialVersionUID = -4896346110499936716L;

    public RecursoDaoImp() {
        super(Recurso.class);
    }

    @Override
    public List<Recurso> findBy(final Perfil perfil) {

        String jpql = "SELECT e FROM Recurso e JOIN e.perfil a WHERE a.id = :perfil_id ";

        Query query = getEM().createQuery(jpql);
        query.setParameter("perfil_id", perfil.getId());

        @SuppressWarnings("unchecked")
        List<Recurso> resultList = query.getResultList();

        if (resultList.isEmpty()) {
            return Collections.emptyList();
        }

        return resultList;
    }

}
